package cn.wy.basic.controller;

import cn.wy.basic.dto.CodeDto;
import cn.wy.basic.service.VerifyService;
import cn.wy.basic.util.AjaxResult;
import cn.wy.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping ("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private VerifyService verifyService;

    @GetMapping ("/image/{key}")
    public String getImgCode(@PathVariable ("key") String key){
            return verifyService.getImgCode(key);
    }

    @PostMapping ("/smsCode")
    public AjaxResult getsmsCode(@RequestBody Map<String,String> map){
        Set<String> set = map.keySet();
        for (String key : set) {
            System.out.println(key+"-----"+map.get(key));
        }
        try {
            verifyService.getsmsCode(map);
            return new AjaxResult();
        }catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统繁忙，请稍后再试！");
        }
    }

    /**
     * 获取手机验证码
     * @return
     */
    @PostMapping("/binderSmsCode")
    public AjaxResult binderVerifyCode(@RequestBody CodeDto codeDto){
        try {
            verifyService.binderVerifyCode(codeDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            //后端打印错误日志
            //e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }
}